package controller

import (
	"github.com/cn-ygf/go-admin/internal/action"
	"github.com/cn-ygf/go-admin/internal/modular/system/service"
	"github.com/cn-ygf/go-admin/internal/modular/system/vo"
	"github.com/cn-ygf/go-admin/pkg/utils"
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
)

// DictTypeController 字典管理
type DictTypeController struct {
	Ctx iris.Context
}

// PostList 查询字典列表
func (c *DictTypeController) PostList(req vo.QueryDictTypeListReq) mvc.Result {
	if !hasPermi(c.Ctx, "system:dict:list") {
		return nil
	}
	var (
		err error
	)
	if req.PageSize > 1000 {
		req.PageSize = 1000
	}
	if req.Params != nil {
		req.Params.BeginTime = req.Params.BeginTime / 1000
		req.Params.EndTime = req.Params.EndTime / 1000
	}
	list, total, err := service.DictTypeService.QueryList(&req)
	if err != nil {
		systemLog.Errorln(err.Error())
		return action.ServerError()
	}
	return action.Data(map[string]any{
		"rows":  list,
		"total": total,
	})
}

// GetBy 根据id获取字典信息
func (c *DictTypeController) GetBy(id int64) mvc.Result {
	if !hasPermi(c.Ctx, "system:dict:query") {
		return nil
	}
	dictType, err := service.DictTypeService.GetByID(id)
	if err != nil {
		systemLog.Errorln(err.Error())
		return action.ServerError()
	}
	return action.Data(dictType)
}

// Put 修改字典
func (c *DictTypeController) Put(req vo.EditDictTypeReq) mvc.Result {
	if !hasPermi(c.Ctx, "system:dict:edit") {
		return nil
	}
	var (
		err error
	)
	if !service.DictTypeService.CheckDictTypeUnique(req.DictId, req.DictType) {
		return action.Error("修改字典'" + req.DictName + "'失败，字典类型已存在")
	}
	req.UpdateBy = getCurrentUser(c.Ctx).UserName
	err = service.DictTypeService.Update(&req)
	if err != nil {
		systemLog.Errorln(err.Error())
		return action.ServerError()
	}
	return action.OK()
}

// Post 添加字典
func (c *DictTypeController) Post(req vo.AddDictTypeReq) mvc.Result {
	if !hasPermi(c.Ctx, "system:dict:add") {
		return nil
	}
	var (
		err error
	)

	if !service.DictTypeService.CheckDictTypeUnique(0, req.DictType) {
		return action.Error("添加字典'" + req.DictName + "'失败，字典类型已存在")
	}
	req.CreateBy = getCurrentUser(c.Ctx).UserName
	err = service.DictTypeService.Add(&req)
	if err != nil {
		systemLog.Errorln(err.Error())
		return action.ServerError()
	}
	return action.OK()
}

// DeleteBy 删除字典
func (c *DictTypeController) DeleteBy(idStr string) mvc.Result {
	if !hasPermi(c.Ctx, "system:dict:remove") {
		return nil
	}
	var (
		err error
	)
	ids := utils.IdsStrToInt64(idStr)
	if len(ids) < 1 {
		return action.ParamError()
	}

	err = service.DictTypeService.Delete(ids)
	if err != nil {
		systemLog.Errorln(err.Error())
		return action.ServerError()
	}
	return action.OK()
}

// PostExport 导出字典
func (c *DictTypeController) PostExport() mvc.Result {
	if !hasPermi(c.Ctx, "system:dict:export") {
		return action.RawError()
	}
	postList, err := service.DictTypeService.All()
	if err != nil {
		systemLog.Errorln(err.Error())
		return action.RawError()
	}
	titleRow := []string{"字典编号", "字典名称", "字典类型", "状态", "创建时间", "备注"}
	statusMaps, err := service.DictDataService.GetDictMaps("sys_normal_disable")
	if err != nil {
		systemLog.Errorln(err.Error())
		return action.RawError()
	}
	data := make([][]any, len(postList))
	for index, item := range postList {
		row := make([]any, 6)
		row[0] = item.DictID
		row[1] = item.DictName
		row[2] = item.DictType
		row[3] = statusMaps[item.Status]
		row[4] = utils.UnixTimeToString(item.CreatedAt)
		row[5] = item.Remark
		data[index] = row
	}
	return action.ExportExcel(data, titleRow)
}

// DeleteCache 清除字典缓存
func (c *DictTypeController) DeleteCache() mvc.Result {
	if !hasPermi(c.Ctx, "system:dict:remove") {
		return nil
	}
	err := service.DictDataService.RefreshCache()
	if err != nil {
		return action.Error(err.Error())
	}
	return action.OK()
}

// GetSelect 获取字典选择框列表
func (c *DictTypeController) GetSelect() mvc.Result {
	list, err := service.DictTypeService.All()
	if err != nil {
		systemLog.Errorln(err.Error())
		return action.ServerError()
	}
	l := len(list)
	result := make([]*vo.SysDictTypeSelect, l)
	for index, item := range list {
		result[index] = &vo.SysDictTypeSelect{
			DictId:   item.DictID,
			DictName: item.DictName,
			DictType: item.DictType,
		}
	}
	return action.Data(result)
}
